package com.sl.plat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sl.common.ResponseData;
import com.sl.water.entity.BThresholdWater;
import com.sl.water.entity.ThresholdRainfall;
import com.sl.water.service.ThresholdRainfallService;
import com.sl.water.vo.BaseVO;
import com.sl.water.vo.ThresholdRainfallIVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plat/threshold/rainfall")
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Tag(name = "预警-降雨阈值接口", description = "提供降雨阈值增、删、改、查、列表查询相关功能")
public class ThresholdRainfallController {
    private final ThresholdRainfallService thresholdRainfallService;

    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public ResponseData<Page<ThresholdRainfall>> pageList(
            @Parameter(
                    description = "页码",
                    example = "1", required = true
            )
            @RequestParam(defaultValue = "1") int page,
            @Parameter(
                    description = "分页数量",
                    example = "10", required = true
            )
            @RequestParam(defaultValue = "10") int size) {
        return ResponseData.success(thresholdRainfallService.pageList(page,size));
    }

    @GetMapping
    @Operation(summary = "查询降雨阈值列表")
    public ResponseData<List<ThresholdRainfall>> getList() {
        return ResponseData.success(thresholdRainfallService.list());
    }

    @PostMapping
    @Operation(summary = "新增降雨阈值")
    public ResponseData save(@RequestBody ThresholdRainfallIVO thresholdRainfallVO) {
        ThresholdRainfall thresholdRainfall = BaseVO.convert(thresholdRainfallVO, ThresholdRainfall.class);
        thresholdRainfallService.save(thresholdRainfall);
        return ResponseData.success(thresholdRainfall.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除降雨阈值")
    public ResponseData removeById(@PathVariable long id) {
        thresholdRainfallService.removeById(id);
        return ResponseData.success("删除成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改降雨阈值")
    public ResponseData updateById(@PathVariable long id,
                                   @RequestBody ThresholdRainfallIVO thresholdRainfallVO) {
        ThresholdRainfall thresholdRainfall = BaseVO.convert(thresholdRainfallVO, ThresholdRainfall.class);
        thresholdRainfall.setId(id);
        thresholdRainfallService.updateById(thresholdRainfall);
        return ResponseData.success("修改成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询降雨阈值")
    public ResponseData<ThresholdRainfall> getById(@PathVariable long id) {
        thresholdRainfallService.getById(id);
        return ResponseData.success(thresholdRainfallService.getById(id));
    }
}
